package org.maven.dependencybrowser;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.codehaus.classworlds.ClassWorld;
import org.codehaus.plexus.embed.Embedder;
import org.maven.dependencybrowser.config.Config;
import org.maven.dependencybrowser.gui.DependencyBrowserGui;
import org.maven.dependencybrowser.implementations.local.LocalDiskRepository;
import org.maven.dependencybrowser.implementations.local.LocalDiskWorkspace;
import org.maven.dependencybrowser.implementations.zip.ZipFileRepository;
import org.maven.dependencybrowser.implementations.zip.ZipFileWorkspace;

import javax.swing.*;
import java.util.Properties;

public class DependencyBrowser
{

    private static final String KEY_TYPE = "type";
    public static final String TYPE_LOCAL = "local";
    private static final String TYPE_ZIP = "zip";

    /**
     * @deprecated - use gui config org.maven.dependencybrowser.config.Config instead
     */
    private static final String ARG_REPOSITORY = "--repository=";
    /**
     * @deprecated - use gui config org.maven.dependencybrowser.config.Config instead
     */
    private static final String ARG_WORKSPACE = "--workspace=";
    private static final String ARG_SNAPSHOT = "--snapshot=";

    public static void main(String[] args) throws Exception
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        // try to configure from the command line
        Properties properties = configureFromCommandLine(args);
        if (properties.get(KEY_TYPE) == null)
        {
            // if no command line config present then use the configuration gui
            Config configDialog = new Config();
            properties = configDialog.configure();
            properties.setProperty(KEY_TYPE, TYPE_LOCAL);
        }

        Embedder embedder = new Embedder();
        ClassWorld classWorld = new ClassWorld();
        classWorld.newRealm("plexus.core", Thread.currentThread().getContextClassLoader());
        embedder.setProperties(properties);
        embedder.start(classWorld);
        DependencyBrowserGui gui = (DependencyBrowserGui) embedder.lookup(DependencyBrowserGui.ROLE);
        gui.open();
        embedder.stop();
    }

    private static Properties configureFromCommandLine(String[] args)
    {
        Properties properties = new Properties();
        for (String arg : args)
        {
            if (arg.startsWith(ARG_REPOSITORY))
            {
                setRepositoryType(properties, TYPE_LOCAL);
                properties.setProperty(LocalDiskRepository.KEY_LOCAL_REPO_PATH, arg.substring(ARG_REPOSITORY.length()));
            }
            if (arg.startsWith(ARG_WORKSPACE))
            {
                setRepositoryType(properties, TYPE_LOCAL);
                properties.setProperty(LocalDiskWorkspace.KEY_WORKSPACE_PATH, arg.substring(ARG_WORKSPACE.length()));
            }
            if (arg.startsWith(ARG_SNAPSHOT))
            {
                setRepositoryType(properties, TYPE_ZIP);
                properties.setProperty(ZipFileRepository.KEY_ZIPFILE_PATH, arg.substring(ARG_SNAPSHOT.length()));
                properties.setProperty(ZipFileWorkspace.KEY_ZIPFILE_PATH, arg.substring(ARG_SNAPSHOT.length()));
            }
        }
        return properties;
    }

    private static void setRepositoryType(Properties properties, String type)
    {
        if (properties.get(KEY_TYPE) == null || properties.get(KEY_TYPE).equals(type))
        {
            properties.setProperty(KEY_TYPE, type);
        }
        else
        {
            throw new IllegalArgumentException("Cannot overwrite configuration type " + properties.get(KEY_TYPE) + " with type " + type);
        }
    }

}
